<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Admin\Renderable\BannerRender;
use App\Admin\Renderable\IconRender;
use App\Admin\Renderable\QuestionRender;
use App\Admin\Renderable\Shop\ShopNoticeRender;
use App\Admin\Renderable\ShopGoodsRender;
use App\Models\Shop\WxShopClassify;
use App\Models\Shop\WxShopGoods;
use App\Models\Shop\WxShopNotice;
use App\Models\WxClause;
use App\Models\WxIcon;
use App\Models\WxQuestion;
use App\Models\WxSlideshow;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formShopTrait{
    public function index_shop(Content $content)
    {
        return $content->title('商城相关页面配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_shop()->render()}</div>
HTML
            );
    }
    protected function form_shop(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->tab('详情', function (Form $form) {

            $form->html(function () {
                return '';
            }, '<b>一、自营商城客服（App）</b>');
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">1、小程序使用微信小程序客服（你可以在微信公众平台找到客服配置，把自己的微信号配置成客服，就可以收到客户的消息提醒）<br/>2、App客服可自定义跳转类型，推荐的是企业微信客服</div>';
            }, '');
            $form->radio('app_shop_servicer_target_type', '跳转类型')->options(FieldUtils::getUrlTypes())->default(Settings::get('app_shop_servicer_target_type', 0));
            $form->text('app_shop_servicer_target_id', '跳转id')->default(Settings::get('app_shop_servicer_target_id', ''));
        });


        $form->tab('好物页面', function (Form $form) {
            $form->html(function () {
                return '<a href="https://img.mini.minisns.cn/images/hbx/shop_preview.png" target="_blank">点击查看，这里设置的是什么地方</a>';
            }, '');

            $form->html(function () {
                return '';
            }, '<b>一、轮播图</b>');
            $form->multipleSelectTable('app_shop_banner', '轮播图')
                ->title('轮播图库')
                ->max(10)
                ->from(BannerRender::make())
                ->model(WxSlideshow::class, 'id', 'id', 'selectTable:formMiniTrait:app_shop_banner')
                ->help('<a href="'.admin_url('slideshow').'" target="_blank">运营管理-轮播图中心</a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')->default(Settings::get('app_shop_banner', '30,32'));

            $form->html(function () {
                return '';
            }, '<b>二、商城通知</b>');

            $form->multipleSelectTable('app_shop_notice', '商城通知')
                ->title('商城通知')
                ->max(10)
                ->from(ShopNoticeRender::make())
                ->model(WxShopNotice::class, 'id', 'title', 'selectTable:formShopTrait:app_shop_notice')
                ->help('<a href="'.admin_url('shop/notice').'" target="_blank">管理商城通知>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_shop_notice', ''));

            $form->html(function () {
                return '';
            }, '<b>三、格子菜单</b>');


            $form->multipleSelectTable('app_shop_nav_', '格子菜单')
                ->title('图标库')
                ->max(10)
                ->from(IconRender::make())
                ->model(WxIcon::class, 'id', 'name', 'selectTable:formShopTrait:app_shop_nav_')
                ->help('这里尽量用图标，才可以适配暗黑模式， <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_shop_nav_', ''));


            $form->html(function () {
                return '';
            }, '<b>四、手风琴</b>');
            $form->radio('is_app_shop_accordion', '手风琴')->options([0=>'否', 1=>'是'])->when(1, function (Form $form){
                $form->multipleSelectTable('app_shop_accordion_goods', '展示商品')
                    ->title('商品库')
                    ->max(6)
                    ->from(ShopGoodsRender::make())
                    ->model(WxShopGoods::class, 'id', 'id')->default(Settings::get('app_shop_accordion_goods', ''));
            })->default(Settings::get('is_app_shop_accordion', 0));

            $form->html(function () {
                return '';
            }, '<b>五、热销榜单</b>');
            $form->radio('is_app_shop_hot_list', '热销榜单')->options([0=>'否', 1=>'是'])->when(1, function (Form $form){
                $form->multipleSelect('app_shop_hot_list_classify', '选择商品分类')->options(WxShopClassify::pluck('name', 'id'))->default(Settings::get('app_shop_hot_list_classify', []));
            })->default(Settings::get('is_app_shop_hot_list', 0));
        });



        $form->tab('店铺申请', function (Form $form) {
            $form->html(function () {
                return '';
            }, '<b>一、协议</b>');
            $form->select('app_shop_apply_clause', '商家服务协议')->options(WxClause::pluck('title', 'id'))->help('<a href="'.admin_url('clause').'" target="_blank">进入条款管理</a>')->default(Settings::get('app_shop_apply_clause', 0));
            $form->html(function () {
                return '';
            }, '<b>一、店铺申请</b>');
            $form->checkbox('app_shop_apply_type_open', '开放的类型')->options([1=> '个人店', 2=> '个体户', 3=>'企业店', 4=>'旗舰店/专卖店'])->help('目前只能开放个人类型，该配置不会生效')->default(Settings::get('app_shop_apply_type_open', '1,2,3,4'));
            $form->html(function () {
                return '';
            }, '<b>三、常见问题</b>');
            $form->multipleSelectTable('app_shop_apply_questions', '常见问题')
                ->title('帮助问题库')
                ->max(10)
                ->from(QuestionRender::make())
                ->model(WxQuestion::class, 'id', 'title', 'selectTable:formShopTrait:app_shop_apply_questions')
                ->help('<a href="'.admin_url('qa/question').'" target="_blank">用户管理-帮助</a>')
                ->default(Settings::get('app_shop_apply_questions', ''));

        });


        $form->action('settings/action/shop');
        return $form;
    }

    public function shop(Request $request){
        SiteUtils::forget_config_data();

        Settings::set('app_shop_servicer_target_type', $request->input('app_shop_servicer_target_type'), true);
        Settings::set('app_shop_servicer_target_id', $request->input('app_shop_servicer_target_id'), true);

        Settings::set('app_shop_apply_questions', AdminUtils::_multipleSelectTable_order_process('selectTable:formShopTrait:app_shop_apply_questions', $request->input('app_shop_apply_questions')), true);
        Settings::set('app_shop_banner', AdminUtils::_multipleSelectTable_order_process('selectTable:formMiniTrait:app_shop_banner', $request->input('app_shop_banner')) , true);
        Settings::set('app_shop_nav_', AdminUtils::_multipleSelectTable_order_process('selectTable:formShopTrait:app_shop_nav_', $request->input('app_shop_nav_')) , true);
        Settings::set('app_shop_notice', AdminUtils::_multipleSelectTable_order_process('selectTable:formShopTrait:app_shop_notice', $request->input('app_shop_notice')) , true);

        Settings::set('is_app_shop_accordion', $request->input('is_app_shop_accordion'), true);
        Settings::set('is_app_shop_hot_list', $request->input('is_app_shop_hot_list'), true);
        Settings::set('app_shop_accordion_goods', $request->input('app_shop_accordion_goods'), true);
        Settings::set('app_shop_hot_list_classify', $request->input('app_shop_hot_list_classify'), true);
        Settings::set('app_shop_apply_clause', $request->input('app_shop_apply_clause'), true);



        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
